# CalHamletV1.py
def getText():
    txt = open("hamlet.txt", "r").read()  # 打开文件
    txt = txt.lower()  # 都变小写字母
    for ch in "'!#$%&()*+,-./\":;<=>?@[\\]^_{|}~":  # 将特殊符号化为空格
        txt = txt.replace(ch, " ")
    return txt


hamletTxt = getText()  # 读取
words = hamletTxt.split()  # 以列表形式分隔开每个单词
counts = {}
for word in words:
    counts[word] = counts.get(word, 0) + 1  # 字典存储并累加
items = list(counts.items())  # 将字典的变为列表
items.sort(key=lambda x: x[1], reverse=True)  # 按照键值对第二个参数进行排序（由大到小）
for i in range(10):  # 挑选前10个单词
    word, count = items[i]
    print("{0:<10}{1:>5}".format(word, count))
